/**
 * @file:          base_math.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.29
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.29,09:27:17
 */

#ifndef _BASE_MATH_H_
#define _BASE_MATH_H_

/* Include Files **************************************************************/
#include "Datatype.h"
#include "math.h"

/* Macro Definition ***********************************************************/
#define BM_PI 3.1415926535897932384626433F
#define BM_2PI 6.2831853071795864769252867F
#define BM_OVER_PI 0.3183098861837906715377675F
#define BM_OVER_2PI 0.1591549430918953357688837F
#define BM_PI_D2 1.5707963267948966192313216F

#define RAD2RPM (60 / BM_2PI)
#define RPM2RAD (BM_2PI / 60)

#define SIN_X_MAX 4096UL               // 水平分辨率
#define SIN_Y_MAX 32767UL              // 垂直分辨率
#define OVER_SIN_Y_MAX (1 / SIN_Y_MAX) // 垂直分辨率倒数
#define sin_bm_x(theta) ((int16)((theta) * BM_OVER_2PI * SIN_X_MAX) & (SIN_X_MAX - 1))
// #define sin_bm_x(theta)       ((int16)((theta)*BM_OVER_2PI*SIN_X_MAX)%(SIN_X_MAX))
// #define sin_bm_x(theta)       ((int16)fmodf(((theta)*BM_OVER_2PI*SIN_X_MAX), SIN_X_MAX))
#define sin_bm_y(theta) (bm_sin_table[sin_bm_x(theta)])
#define sin_bm(theta) ((float32)sin_bm_y(theta) / SIN_Y_MAX)
#define cos_bm(theta) ((float32)sin_bm_y(BM_PI_D2 - theta) / SIN_Y_MAX)
// #define sin_tbl(theta)       ((int16)((theta)*BM_OVER_2PI*SIN_X_MAX)%SIN_X_MAX)
// #define sin_tbl(theta)       (sin_pi_m2[(theta*BM_OVER_2PI*SIN_X_MAX)%SIN_X_MAX])

#define squre(x) ((x) * (x))
#define Limit(in, min, max) ((in) < (min) ? (min) : (in > (max) ? (max) : (in)))
#define sign_bm(x) (x > 0 ? 1L : (x < 0 ? -1L : 0L))
#define fsg_bm(x, d) ((sign_bm(x + d) - sign_bm(x - d)) / 2.0F)
#define abs_bm(x) ((x) >= 0 ? (x) : -(x))

/* Data Type Define ***********************************************************/

/* Variable Declare ***********************************************************/
// extern const int16 sin_pi_m2[sin_pi_m2_dpix + 1];
extern const int16 bm_sin_table[];

/* Function Declare ***********************************************************/
void bm_sin_test();
void bm_sin_wave_test();
float bm_sqrt(float x);
float bm_sqrt_carmack(float number);
float bm_sin_fast(float x);
float bm_cos_fast(float x);
float KahanSummation_Sum();
float fsg0(float x, float d);
float fsg(float x, float a, float b);
float fdb0(float x, float d);
float fss(float x, float a, float b);
float fst0(float x, float d);
float fal(float x, float a, float d);
uint8_t crc8_cal(const uint8_t *data, uint32_t len);
#endif